package org.tio.sitexxx.web.server.controller.minip;

import org.tio.http.common.HttpRequest;
import org.tio.http.server.annotation.RequestPath;
import org.tio.sitexxx.service.service.qj.SearchService;

import java.util.HashMap;
import java.util.Map;


@RequestPath(value = "/search")
public class SearchController {
    /**
     * 搜索商品
     * @param request
     * @return
     */
    @RequestPath(value = "/list")
    public Map<String, Object> list(HttpRequest request) {
        Map<String, Object> resultMap = new HashMap<>();
        Map<String, Object> query = new HashMap<>();
        String type=null,value=null;Integer pageNum=null;
        try{
             type=request.getParam("type");
             value=request.getParam("value");
             pageNum=Integer.valueOf(request.getParam("pageNum"));
        }catch (NullPointerException e){
            resultMap.put("c", "参数错误");
            return resultMap;
        }catch (Exception e){
            resultMap.put("c","Unknow error");
            return resultMap;
        }
        System.out.println("called search ");

        query.put("value", value);//搜索值
        query.put("pageNum", pageNum*12);//分页
        if (type.equals("商品") ) {
            resultMap.put("c", SearchService.me.getCommoditys(query));
        } else if (type.equals("文章")) {
            resultMap.put("c", SearchService.me.getKnowledges(query));
        } else if (type.equals("顾问")) {
            resultMap.put("c", SearchService.me.getConsultants(query));
        }

        return resultMap;
    }

}
